package com.yao.ro.service.impl;


import java.math.BigDecimal;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yao.common.enums.business.ro.FinishStatusEnum;
import com.yao.common.exception.BusinessException;
import com.yao.common.util.ThrowUtils;
import com.yao.ro.entity.dto.EquipmentListDto;
import com.yao.ro.entity.dto.ExchangeDto;
import com.yao.ro.entity.vo.EquipmentListVo;
import com.yao.ro.mapper.EquipmentListMapper;
import com.yao.ro.mapper.sql.EquipmentListWrapper;
import com.yao.ro.service.EquipmentListService;
import com.yao.ro.service.ExchangeService;
import com.yao.ro.util.PriceUtils;

import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.ObjectUtil;

/**
* 装备材料表 接口实现
*
* @author autoCode
* @version 1.0.0-SNAPSHOT
* @date 2020-04-22
*/
@Service
public class EquipmentListServiceImpl extends ServiceImpl<EquipmentListMapper, EquipmentListDto> implements EquipmentListService{

    @Autowired
    private EquipmentListMapper mapper;

    @Autowired
    private ExchangeService exchangeService;


    @Override
    @Transactional(isolation = Isolation.REPEATABLE_READ, rollbackFor = BusinessException.class)
    public void add(EquipmentListDto dto) {

        // 合并重复材料
        Wrapper<EquipmentListDto> query = EquipmentListWrapper.byEqmIdNGoodsId(dto.getEqmId(), dto.getGoodsId());
        EquipmentListDto one = this.getOne(query);

        Integer needs = dto.getNeeds();
        Integer remaining = dto.getRemaining();

        if(ObjectUtil.isNotNull(one)){
            needs = NumberUtil.add(needs, one.getNeeds()).intValue();
            dto.setNeeds(needs);

            remaining = NumberUtil.add(remaining, one.getRemaining()).intValue();
            dto.setRemaining(remaining);

            dto.setId(one.getId());
        }



        if (!this.saveOrUpdate(dto)) {
            throw new BusinessException("新增或更新材料失败");
        }
    }

    @Override
    public EquipmentListDto byId(Integer id) {

        EquipmentListDto dto = ThrowUtils.isNull(this.getById(id), "材料清单不存在.{}", id);
        
        final Integer goodsId = dto.getGoodsId();

        ExchangeDto exchange = ThrowUtils.isNull(exchangeService.getByGoodsId(goodsId), "查询不到商品交易所信息,goodsId:{}", goodsId);
        
        dto.setPrice(exchange.getPrice());

        return dto;
    }

    @Override
    public IPage<EquipmentListVo> page(IPage<EquipmentListDto> page, EquipmentListDto dto) {

        IPage<EquipmentListVo> page1 = mapper.page(page, dto);

        page1.getRecords().forEach(e -> {

            /*
             *  最高价 = 当前价 = 最低价 ==> 无变化0
             *  最高价 = 当前价 > 最低价 ==> 最高价变色 1
             *  最高价 > 当前价 = 最低价 ==> 最低价变色 2
             */
            BigDecimal price = new BigDecimal(e.getPrice());
            BigDecimal maxPrice = new BigDecimal(e.getHistoryMaxPrice());
            BigDecimal minPrice = new BigDecimal(e.getHistoryMinPrice());

            int change = 0;

            if(maxPrice.compareTo(price) == 0 && minPrice.compareTo(price) == 0){
                change = 0;
            }else if(maxPrice.compareTo(price) == 0 && minPrice.compareTo(price) == -1){
                change = 1;
            }else if(minPrice.compareTo(price) == 0 && maxPrice.compareTo(price) == 1){
                change = 2;
            }

            e.setChange(change);

            e.setPrice(PriceUtils.formatter(e.getPrice()));
            e.setHistoryMaxPrice(PriceUtils.formatter(e.getHistoryMaxPrice()));
            e.setHistoryMinPrice(PriceUtils.formatter(e.getHistoryMinPrice()));

        });

        return page1;
    }

    @Override
    @Transactional(isolation = Isolation.REPEATABLE_READ, rollbackFor = BusinessException.class)
    public void updateRemaining(EquipmentListDto dto) {
        if (dto.getRemaining() == 0) {
            dto.setCollectStatus(FinishStatusEnum.FINISHED.getCode());
        }

        this.updateById(dto);
    }

    @Override
    public void updatePrice(EquipmentListDto dto) {

        EquipmentListDto equipmentListDto = this.getById(dto.getId());

        exchangeService.updatePriceByGoodsId(equipmentListDto.getGoodsId(), dto.getPrice());

    }
}